/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.perekamananggarankegiatanunit;

import org.sadhar.sia.framework.ClassApplicationModule;
import org.sadhar.sipp.posbiaya.PosBiaya;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author hendro
 */
public class PosBiayaPopupWnd extends ClassApplicationModule {

    private Listbox lstboxPosBiaya;
    private Textbox txtboxKodeKelompokKegiatan;
    private Textbox txtboxTahunAnggaran;
    private Textbox txtboxKeySearch;
    private PerekamanAnggaranKegiatanUnitDAO kegiatanDAO;

    public PosBiayaPopupWnd() {
        this.kegiatanDAO = new PerekamanAnggaranKegiatanUnitDAOImpl();
    }

    public void onCreate() throws Exception {
        lstboxPosBiaya = (Listbox) getFellow("lstboxPosBiaya");
        txtboxKodeKelompokKegiatan = (Textbox) getFellow("txtboxKodeKelompokKegiatan");
        txtboxTahunAnggaran = (Textbox) getFellow("txtboxTahunAnggaran");
        txtboxKeySearch = (Textbox) getFellow("txtboxKeySearch");
        loadPosBiaya();
    }

    private void loadPosBiaya() throws Exception {
        lstboxPosBiaya.getItems().clear();
        for (PosBiaya posBiaya : kegiatanDAO.listPosBiaya(Integer.valueOf(txtboxKodeKelompokKegiatan.getValue()), txtboxTahunAnggaran.getValue(), txtboxKeySearch.getValue())) {
            Listitem item = new Listitem();
            item.setValue(posBiaya);
            item.appendChild(new Listcell(posBiaya.getNamaPosBiaya()));
            lstboxPosBiaya.appendChild(item);
        }
    }

    public void lstboxPosBiayaOnSelect() throws Exception {
        PosBiaya posBiaya = (PosBiaya) lstboxPosBiaya.getSelectedItem().getValue();
        Window window = (Window) this.getRoot().getFellow("contentDiv").getFirstChild();
        Label lblPosBiaya = (Label) window.getFellow("lblPosBiaya");
        lblPosBiaya.setValue(posBiaya.getNamaPosBiaya());
        Button btnCariJenis = (Button) window.getFellow("btnCariJenis");
        Button btnCariBarang = (Button) window.getFellow("btnCariBarang");
        Textbox txtboxKodePosBiaya = (Textbox) window.getFellow("txtboxKodePosBiaya");
        Textbox txtboxCariJenis = (Textbox)window.getFellow("txtboxCariJenis");
        Button btnCariItemBiaya = (Button) window.getFellow("btnCariItemBiaya");
        txtboxKodePosBiaya.setValue(posBiaya.getKodePosBiaya() + "");
        if (kegiatanDAO.selectNamaTableItem(posBiaya.getKodePosBiaya()).equalsIgnoreCase("itemBiaya")) {
            btnCariJenis.setVisible(false);
            txtboxCariJenis.setValue("");
            txtboxCariJenis.setDisabled(true);
            btnCariItemBiaya.setVisible(true);
            btnCariBarang.setVisible(false);
        } else if (kegiatanDAO.selectNamaTableItem(posBiaya.getKodePosBiaya()).equalsIgnoreCase("barang")) {
            btnCariJenis.setVisible(true);
            btnCariBarang.setVisible(true);
            txtboxCariJenis.setValue("");
            txtboxCariJenis.setDisabled(false);
            btnCariItemBiaya.setVisible(false);
        }
        this.detach();
    }
}
